//
//  MessageViewController.m
//  AILove
//
//  Created by 甘世清 on 2023/12/15.
//

#import "MessageViewController.h"
#import "MessageListTableViewCell.h"
#import "ChatViewController.h"
#import "HistoryModel.h"
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation MessageViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count<= 0) {
        [self SQ_Header];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"消息");
}

- (void)SQ_UIViewForSelfView{
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView * image =  [[UIImageView alloc]init];
    image.image = SQGetImage(@"模糊背景");
    image.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
}

- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:Url8 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        self.jsonArray = [HistoryModel mj_objectArrayWithKeyValuesArray:httpModel.data[@"records"]];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:Url8 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageListTableViewCell"];
    if (!cell) {
        cell = [[MessageListTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MessageListTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.jsonArray[indexPath.row];
    return cell;
}

//iOS11 后的新方法,,  可以设置image和title
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    //删除
    WS(weakSelf);
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:SQNSLocalString(@"delete") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        HistoryModel * model = weakSelf.jsonArray[indexPath.row];
        [weakSelf deleteChatRoom:model.roomId];
//        self.selectIndex = indexPath.row;
//        UIAlertController * alert = [UIAlertController initWithType:AceAlertViewStyleAlert andTitleImage:@"Delete 1" andTitle:ZBLocalized(@"Warning") andMessage:ZBLocalized(@"Do you want to delete the selected record, the chat history with this character cannot be restored after deletion") andConfirmName:@"OK" andCancleName:@"取消"];
//        alert.delegate = self;
//        [alert show];
//        completionHandler (YES);
    }];
    // 设置删除按钮的图标
    deleteRowAction.title = SQNSLocalString(@"delete");
    
    deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"#FC5A55"];
    
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
}

-(void)deleteChatRoom:(NSString *)roomId{
    NSDictionary * dic = @{
        @"roomId":roomId
    };
    [HttpTool post:SCJL paramDic:[NSMutableDictionary dictionaryWithDictionary:dic] successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMessage:SQNSLocalString(@"成功") afterDelaytime:2];
        [self SQ_Header];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMessage:httpModel.message afterDelaytime:2];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     return  82;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HistoryModel * model = self.jsonArray[indexPath.row];
    ChatViewController * vc = [ChatViewController new];
    vc.roomID = model.roomId;
    vc.photoURL = model.cover;
    vc.title = model.name;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
